<template>
	<view class="content">
		<view class="status_bar">
			<!-- 这里是状态栏 -->
		</view>
		<view class="search-box">
			<view class="input-box">
				<img src="~/static/icon/search(2).png">
				<input type="text" :placeholder="defaultKeyword" @input="inputChange" v-model="keyword" @confirm="doSearch(false)"
				 placeholder-class="placeholder-class" confirm-type="search">
				<view class="delete" v-if="isShowDelete" @click="empty">
					<img src="~/static/icon/delete.png">
				</view>
			</view>
			<view class="search-btn" @tap="noSearch(false)">取消</view>
		</view>
		<view class="search-keyword" @touchstart="blur">
			<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
				<view class="keyword-entry" hover-class="keyword-entry-tap" v-for="row in keywordList" :key="row.vod_id"  @tap="doSearch(row.vod_name,row)">
					<view class="keyword-text">
						<rich-text :nodes="row.vod_name"></rich-text>
					</view>
				</view>
			</scroll-view>
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<view class="keyword-block" v-if="oldKeywordList.length>0">
					<view class="keyword-list-header">
						<view>历史搜索</view>
						<view>
							<image @tap="oldDelete" src="/static/HM-search/delete.png"></image>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">{{keyword}}</view>
					</view>
				</view>
				<view class="keyword-block">
					<view class="keyword-list-header">
						<view>热门搜索</view>
						<view>
							<image @tap="hotToggle" :src="'/static/HM-search/attention'+forbid+'.png'"></image>
						</view>
					</view>
					<view class="keyword" v-if="forbid==''">
						<view v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword.vod_name,keyword)" :key="index">{{keyword.vod_name}}</view>
					</view>
					<view class="hide-hot-tis" v-else>
						<view>当前搜热门搜索已隐藏</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import mSearch from '@/components/mehaotian-search-revision/mehaotian-search-revision.vue';
	export default {
		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [],
				hotKeywordList: [],
				keywordList: [],
				forbid: '',
				isShowKeywordList: false,
				isShowDelete: false
			}
		},
		onLoad() {
			this.init();
			uni.request({
				url: 'http://221.234.36.126:82/api.php/Login/visitor_login',
				method: 'POST',
				data: {
					token: '1CECE2221211DDCB613882C3311EC670'
				},
				header: {
					'content-type': 'application/json;charset=UTF-8'
				},
				success: (res) => {
					// console.log(res);
					let visitor = res.data.data.hash;
					uni.request({
						url: 'http://221.234.36.126:82/api.php/Vod/vod_search_hot',
						method: 'POST',
						data: {
							token: '1CECE2221211DDCB613882C3311EC670',
							hash: visitor
						},
						header: {
							'content-type': 'application/json;charset=UTF-8'
						},
						success: (res) => {
							// console.log(res)
							this.hotKeywordList=res.data.data
							// this.movieList = res.data.list
							// this.classifyList = res.data.cate_child
						}
					})
				}
			});
		},
		components: {
			mSearch
		},
		methods: {
			init() {
				this.loadDefaultKeyword();
				this.loadOldKeyword();
				this.loadHotKeyword();
			},
			blur() {
				uni.hideKeyboard()
			},
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "搜索视频";
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						// console.log(res)
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//加载热门搜索
			loadHotKeyword() {
				//定义热门搜索关键字，可以自己实现ajax请求数据再赋值
				// this.hotKeywordList = ['张一山', '哪吒之魔童降世', '东宫', '搜索', '甄嬛传', '陈情令'];
			},
			//监听输入
			inputChange(event) {
				// console.log(event)
				//兼容引入组件时传入参数情况
				var keyword = event.detail ? event.detail.value : event;
				// console.log(keyword)
				if (!keyword) {
					this.keywordList = [];
					this.isShowKeywordList = false;
					return;
				}
				this.isShowKeywordList = true;
				this.isShowDelete = true
				// console.log(this.keywordList)
				uni.request({
					url: 'http://221.234.36.126:82/api.php/Login/visitor_login',
					method: 'POST',
					data: {
						token: '1CECE2221211DDCB613882C3311EC670'
					},
					header: {
						'content-type': 'application/json;charset=UTF-8'
					},
					success: (res) => {
						let visitor = res.data.data.hash;
						uni.request({
							url: 'http://221.234.36.126:82/api.php/Vod/vod_search',
							method: 'POST',
							data: {
								token: '1CECE2221211DDCB613882C3311EC670',
								hash: visitor,
								keys:keyword
							},
							header: {
								'content-type': 'application/json;charset=UTF-8'
							},
							success: (res) => {
								console.log(res)
								let keywordBox1=res.data.data.vod_list1.list
								let keywordBox2=res.data.data.vod_list2.list
								let keywordBox3=res.data.data.vod_list3.list
								let keywordBox4=res.data.data.vod_list4.list
								this.keywordList=keywordBox1.concat(keywordBox2,keywordBox3,keywordBox4)
								console.log(this.keywordList)
								this.keywordList.forEach(e=>{
									let vodName=e.vod_name
									// console.log(vodName)
									// this.keywordList = this.drawCorrelativeKeyword(vodName, keyword);
								})
							}
						});
					}
				});
				
				
				
			},
			//高亮关键字
			// drawCorrelativeKeyword(keywords, keyword) {
			// 	console.log(keywords)
			// 	var len = keywords.length
			// 		keywordArr = [];
				// for (var i = 0; i < len; i++) {
				// 	var row = keywords[i];
				// 	//定义高亮#00A7FF
				// 	var html = row[0].replace(keyword, "<span style='color: #00A7FF;'>" + keyword + "</span>");
				// 	html = '<div>' + html + '</div>';
				// 	var tmpObj = {
				// 		keyword: row[0],
				// 		htmlStr: html
				// 	};
				// 	keywordArr.push(tmpObj)
				// 	console.log(keywordArr)
				// }
				// return keywordArr;
			// },
			//顶置关键字
			setkeyword(data) {
				this.keyword = data.keyword;
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '_forbid';
			},
			//执行搜索
			doSearch(key,row) {
				console.log(key,row)
				key = key ? key : this.keyword ? this.keyword : this.defaultKeyword;
				this.keyword = key;
				this.saveKeyword(key); //保存为历史 
				uni.showToast({
					title: key,
					icon: 'none',
					duration: 2000
				});
				let vodId=row.vod_id
				//实现搜索逻辑
				uni.navigateTo({
					url: '/pages/videoInfo/videoInfo?id='+vodId
				});
			},
			// 取消搜索
			noSearch() {
				uni.navigateBack({
					delta: 1
				});
			},
			// 清空输入框
			empty() {
				this.keyword = ''
				this.keywordList = [];
				this.isShowKeywordList = false;
			},
			//保存关键字到历史记录
			saveKeyword(keywords) {
				
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						console.log(res.data);
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keywords);
						if (findIndex == -1) {
							OldKeys.unshift(keywords);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keywords);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keywords];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			}
		}
	}
</script>
<style>
	view {
		display: block;
	}

	.search-box {
		width: 750upx;
		height: 128upx;
		padding: 0 30upx;
		background-color: #FFFFFF;
		display: flex;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
		border-bottom: 1px solid #F2F2F2;
	}

	.search-box .mSearch-input-box {
		width: 100%;

	}

	.search-box .input-box {
		width: 587upx;
		height: 66upx;
		background: rgba(242, 242, 242, 1);
		border-radius: 33upx;
		padding: 15upx 22upx;
		flex-shrink: 1;
		display: flex;
		justify-content: center;
		/* align-items: center; */
		box-sizing: border-box;
	}

	.search-box .input-box img {
		width: 36upx;
		height: 36upx;
		margin-right: 15upx;
		vertical-align: middle;
	}

	.search-box .input-box .delete {
		margin-right: 0;
	}

	.search-box .search-btn {
		margin-left: 35upx;
		flex-shrink: 0;
		font-size: 36upx;
		color: rgba(0, 144, 255, 1);
	}

	.search-box .input-box>input {
		width: 100%;
		font-size: 34upx;
	}

	.placeholder-class {
		font-size: 34upx;
		color: rgba(207, 207, 207, 1);
	}


	.search-keyword {
		width: 100%;
		background-color: rgb(242, 242, 242);
	}

	.keyword-list-box {
		/* padding-top: 10upx; */
		background-color: #fff;
	}

	.keyword-entry-tap {
		background-color: #eee;
	}

	.keyword-entry {
		margin: 0 30upx;
		padding: 0 20upx;
		height: 88upx;
		font-size: 30upx;
		color: #515151;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1upx #F2F2F2;
		box-sizing: border-box;
	}

	.keyword-entry image {
		width: 60upx;
		height: 60upx;
	}

	.keyword-entry .keyword-text {
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {}


	.keyword-box {
		background-color: #fff;
	}

	.keyword-box .keyword-block {
		padding: 39upx 30upx 0;
		box-sizing: border-box;
	}

	.keyword-box .keyword-block .keyword-list-header {
		padding-bottom: 36upx;
		font-size: 32upx;
		color: rgba(81, 81, 81, 1);
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
	}

	.keyword-box .keyword-block .keyword-list-header image {
		width: 38upx;
		height: 38upx;
	}

	.keyword-box .keyword-block .keyword {
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}

	.keyword-box .keyword-block .hide-hot-tis {
		display: flex;
		justify-content: center;

	}

	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60upx;
		padding: 0 20upx;
		margin: 10upx 40upx 33upx 0;
		height: 54upx;
		font-size: 26upx;
		background: rgba(247, 247, 247, 1);
		color: rgba(81, 81, 81, 1);
	}
</style>
